package com.aisb.manage;

import com.aisb.core.InitParam;
import com.aisb.core.Result;
import com.aisb.core.SessionBean;
import com.aisb.core.log.LogVisit;
import com.aisb.core.util.DateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/manage")
public class ManageController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    SessionDAO sessionDAO;

    @RequestMapping(value = "/index")
    @RequiresPermissions("/manage/index")
    @LogVisit("系统管理首页")
    public String index(Model model) {
        return "manage/index";
    }


    @RequestMapping(value = "/onlineuser/index")
//    @RequiresPermissions("manage:index")
    @LogVisit("在线用户管理")
    public String onlineUser(Model model) {
        Collection<Session> sessions = sessionDAO.getActiveSessions();

        List rList = new ArrayList();
        Map m;
        for (Session s : sessions) {
            String startTime = DateUtil.getInstance().dateFormat(s.getStartTimestamp(), InitParam.FORMAT_TIME);
            String lastAccessTime = DateUtil.getInstance().dateFormat(s.getLastAccessTime(), InitParam.FORMAT_TIME);
            SimplePrincipalCollection o = (SimplePrincipalCollection) s.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
            if (null == o) {
                continue;
            }
            SessionBean sessionBean = (SessionBean) (o).getPrimaryPrincipal();

            m = new HashMap();
            m.put("id", s.getId());
            m.put("ip", sessionBean.getIp());
            m.put("ipAddress", sessionBean.getIpAddress());
            m.put("startTime", startTime);
            m.put("lastAccessTime", lastAccessTime);
            m.put("userId", sessionBean.getUserId());
            m.put("userName", sessionBean.getUserName());
            rList.add(m);
        }

        model.addAttribute("onlines", rList);

        return "manage/onlineuser";
    }

    @RequestMapping(value = "/index/syscount")
    @ResponseBody
    public Result sysCount(@RequestParam Map param) {
        Map rMap = new HashMap();

        String sql = "select count(*) userCount from ai_user";
        Map m = jdbcTemplate.queryForMap(sql);
        rMap.putAll(m);

        sql = "select count(*) roleCount from ai_role";
        m = jdbcTemplate.queryForMap(sql);
        rMap.putAll(m);

        sql = "select count(*) funcCount from ai_func";
        m = jdbcTemplate.queryForMap(sql);
        rMap.putAll(m);

        sql = "select count(*) noticeCount from ai_notice";
        m = jdbcTemplate.queryForMap(sql);
        rMap.putAll(m);

        sql = "select count(*) logVisitCount from ai_log_visit";
        m = jdbcTemplate.queryForMap(sql);
        rMap.putAll(m);

        sql = "select count(*) logOperCount from ai_log_operator";
        m = jdbcTemplate.queryForMap(sql);
        rMap.putAll(m);

        return new Result(true, rMap);
    }

    @RequestMapping(value = "/index/logvisittrend")
    @ResponseBody
    public Result logVisitTrend(@RequestParam Map param) {
        String sql = "select substring(t.log_time, 1, 10) as rkey, count(*) as rvalue from ai_log_visit t group by substring(t.log_time, 1, 10)";
        List<Map<String, Object>> rList = jdbcTemplate.queryForList(sql);

        List x = new ArrayList();
        List y = new ArrayList();
        for (Map<String, Object> m : rList) {
            String rkey = m.get("rkey").toString();
            String rvalue = m.get("rvalue").toString();
            x.add(rkey);
            y.add(rvalue);
        }

        Map rMap = new HashMap();
        rMap.put("x", x);
        rMap.put("y", y);

        return new Result(true, rMap);
    }

    @RequestMapping(value = "/index/loginhour")
    @ResponseBody
    public Result loginHour(@RequestParam Map param) {
        String sql = "select substring(t.log_time, 12, 2) as rkey, count(*) as rvalue from ai_log_visit t where t.log_content like '%登录%' group by substring(t.log_time, 12, 2)";
        List<Map<String, Object>> rList = jdbcTemplate.queryForList(sql);

        Map hmap = new HashMap();
        for (Map<String, Object> m : rList) {
            hmap.put(m.get("rkey"), m.get("rvalue"));
        }

        List x = new ArrayList();
        List y = new ArrayList();
        for (int i = 0; i < 24; i++) {
            String key = i + "";
            if (key.length() == 1) {
                key = "0" + key;
            }
            x.add(key);
            String rvalue = hmap.get(key) == null ? "0" : hmap.get(key).toString();
            y.add(rvalue);
        }

        Map rMap = new HashMap();
        rMap.put("x", x);
        rMap.put("y", y);

        return new Result(true, rMap);
    }
}
